package com.arashivision.onecamerademo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class GdbServerService extends Service {
    private static final String TAG = "GdbServerService";
    public GdbServerService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startGdbServer();
        return START_STICKY_COMPATIBILITY;
    }

    private void startGdbServer() {
        try {
            String gdbserverPath = getApplicationInfo().nativeLibraryDir + "/gdbserver.so";
            Log.i(TAG, "start gdbserver: " + gdbserverPath);
            new ProcessBuilder()
                    .command(gdbserverPath, "--attach"
                            , ":5050", "" + android.os.Process.myPid())
                    .redirectErrorStream(true)
                    .start();
        } catch (Exception e) {
            Log.e(TAG, "IOException failed to start gdbserver");
        }
    }
}
